// pages/msg_procedure_add/msg_procedure_add.js
let app = getApp();


Page({

    /**
     * 页面的初始数据
     */
    data: {
        array1: [{
                'LCODE': '1',
                'LNAME': '一车间'
            },
            {
                'LCODE': '2',
                'LNAME': '二车间'
            },
            {
                'LCODE': '3',
                'LNAME': '三车间'
            },
            {
                'LCODE': '4',
                'LNAME': '四车间'
            },
            {
                'LCODE': '5',
                'LNAME': '五车间'
            },
        ],
        LPRCCODE: "",
        LPRCNAME: "",
        LCONVERSION: "",
        LFARM: "",
        LPRCCODE: "",
        LMAKEOPID: "",
        LREMARK: "",
        LISUSED: true,
    },
    switch1Change: function (e) {
        this.setData({
            LISUSED: e.detail.value
        })
    },
    bindinputip(e) {
        if (e.currentTarget.dataset.id == '1') {
            this.data.LCONVERSION = e.detail.value
        } else if (e.currentTarget.dataset.id == '2') {
            this.data.LFARM = e.detail.value
        } else if (e.currentTarget.dataset.id == '3') {
            this.data.LREMARK = e.detail.value
        } else if (e.currentTarget.dataset.id == '4') {
            this.data.LPRCNAME = e.detail.value
        }
    },
    bindPickerChange1(e) { // 车间
        this.setData({
            index1: e.detail.value,
        })
        for (var i = 0; i < this.data.array1.length; i++) {
            if (i == e.detail.value) {
                this.setData({
                    LFARM: this.data.array1[i].LCODE
                })
            }
        }
    },
    update(e) {
        let that = this
        if (that.data.LPRCNAME == '') {
            wx.showModal({
                title: '提示',
                content: '请输入名称',
                showCancel: false,
                confirmText: '确定',
                success(res) {
                    if (res.confirm) {

                    }
                }
            })
        } else if (that.data.LFARM == '') {
            wx.showModal({
                title: '提示',
                content: '请选择车间',
                showCancel: false,
                confirmText: '确定',
                success(res) {
                    if (res.confirm) {

                    }
                }
            })
        } else if (that.data.LCONVERSION == '') {
            wx.showModal({
                title: '提示',
                content: '请输入转换率',
                showCancel: false,
                confirmText: '确定',
                success(res) {
                    if (res.confirm) {

                    }
                }
            })
        } else {
            let table = {
                'table': [{
                    // 'LPRCCODE': that.data.LPRCCODE, // 编号
                    'LPRCNAME': that.data.LPRCNAME, //名称
                    'LCONVERSION': that.data.LCONVERSION, //转换率
                    'LFARM': that.data.LFARM, //车间
                    'LMAKEOPID': app.globalData.lusercode, //制单人
                    'LREMARK': that.data.LREMARK, //备注
                    'LISUSED': that.data.LISUSED, //是否有效
                }]
            }
            wx.request({
                url: 'https://' + app.globalData.url + '/Process/addProcess',
                data: {
                    "model": JSON.stringify(table),
                },
                method: 'POST',
                dataType: 'json',
                responseType: 'text',
                header: {
                    'content-type': 'application/x-www-form-urlencoded'
                },
                success: function (res) {
                    if (res.data.success == true) {
                        wx.navigateBack({
                            delta: 1
                        })
                        wx.showToast({
                            title: '成功',
                            icon: 'success',
                            duration: 2000
                        })
                    } else {
                        wx.showToast({
                            title: '添加失败',
                            icon: 'none',
                            duration: 2000
                        })
                    }
                },
                fail: function (res) {
                    that.setData({
                        loadModal: false
                    })
                    wx.showModal({
                        title: '提示',
                        content: '访问服务器出错',
                        showCancel: false,
                        confirmText: '确定',
                        success(res) {
                            if (res.confirm) {

                            }
                        }
                    })
                },
            })
        }

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this
        var date = new Date()
        var year = date.getFullYear()
        var month = date.getMonth() + 1
        var day = date.getDate()
        var lw = new Date(date - 1000 * 60 * 60 * 24 * 30) //最后一个数字30可改，30天的意思

        var date1 = new Date();
        var date3 = new Date(date1);
        date3.setDate(date1.getDate() + 30);
        var enddate = (date3.getFullYear() + "-" + ((date3.getMonth() + 1) < 10 ? '0' + (date3.getMonth() + 1) : (date3.getMonth() + 1)) + "-" + (date3.getDate() < 10 ? '0' + date3.getDate() : date3.getDate()))
        var lastY = lw.getFullYear()
        var lastM = lw.getMonth() + 1
        var lastD = lw.getDate()
        var startdate = lastY + '-' + (lastM < 10 ? '0' + lastM : lastM) + '-' + (lastD < 10 ? '0' + lastD : lastD) //三十天之前日期
        if (month < 10) {
            month = '0' + month
        }
        if (day < 10) {
            day = '0' + day
        }
        var dateright = year + '-' + month + '-' + day

        that.setData({
            dateright: dateright,
            LMAKEOPID: app.globalData.lusername
        })
        that.setData({
            dateleft: startdate
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})